#! /usr/bin/env python3
import sys
import solid as s
import solid.utils as u
from solid import scad_render_to_file
from solid import screw_thread
from solid.objects import cylinder
from mytools import render

class Mybox:
    def __init__(self):
        self.alpha=1
        self.t=2.94
        self.hole=32.19
        self.h_r=0.95
        self.screw=3.4

        self.h=54.7*self.alpha
        self.l=138.06*self.alpha
        self.w=100.37*self.alpha

    def convex(self):
        d=4/2
        l=self.hole*self.h_r
        cy=s.cylinder(r=d/2,h=self.t,center=True)
        cube=u.translate([-d/2,0,0])(s.cube([d,d,self.t],center=True))
        right_cy=u.right(l/2)(cy*cube)
        left_cy=u.left(l/2)(u.rotate((0,0,180))(cy*cube))
        m = s.cube([l,self.t*3,self.t], center=True)
        return m-right_cy-left_cy
    def bottom(self):
        fill=s.cube([self.t,self.hole*self.h_r,self.t],center=True)
        m = s.cube([self.l,self.w,self.t], center=True)
        convex1=u.translate([0,self.t*1.5+self.w/2,0])(self.convex())
        convex2=u.translate([0,-self.t*1.5-self.w/2,0])(self.convex())
        fill_left=u.translate([-0.5*self.t-self.l/2,0,0])(fill)
        fill_right=u.translate([self.t*0.5+self.l/2,0,0])(fill)
        res=m+fill_left+fill_right+convex1+convex2
        return res
    def back(self):
        m = s.cube([self.l,self.h,self.t], center=True)
        margin=1.5*self.t

        hole1=s.cube([self.hole,self.t,self.t],center=True)
        h1=u.translate([0,-self.h/2+margin,0])(hole1)
        h2=u.translate([0,self.h/2-margin,0])(hole1)

        hole2=s.cube([self.t,self.hole,self.t],center=True)
        h3=u.translate([-self.l/2+margin,0,0])(hole2)
        h4=u.translate([self.l/2-margin,0,0])(hole2)
        res=m-h1-h2-h3-h4
        return res
    def side(self):
        m = s.cube([self.w, self.h, self.t], center=True)
        convex = u.rotate([0,0,90])(self.convex())
        c1=u.right(-self.w/2-1.5*self.t)(convex)
        c2=u.right(self.w/2+1.5*self.t)(convex)
        hole=s.cube([self.hole,self.t,self.t],center=True)
        h1=u.translate([0,-self.h/2+1.5*self.t])(hole)
        h2=u.translate([0,self.h/2-1.5*self.t])(hole)
        res=m+c1+c2-h1-h2
        return res
    def up(self):
        m=self.bottom()
        r=self.screw
        l=self.l  * 0.85
        w=self.w  * 0.85
        cy=s.cylinder(r=r,h=self.t,center=True)
        rows=int(l/(r*4))+2
        cols=int(w/(r*4))+1
        for row in range(rows):
            for col in range(cols):
                x = -l / 2 + row * r * 4
                y=-w/2+col*r*4
                m=m-u.translate([x,y,0])(cy)
        return m




if __name__ == '__main__':
    m=Mybox()
    render(m.up())



